package com.cn.springcloudshop.service.impl;

import com.cn.springcloudshop.mapper.YmShopCarMapper;
import com.cn.springcloudshop.mapper.YmShopProductMapper;
import com.cn.springcloudshop.model.YmShopCar;
import com.cn.springcloudshop.model.YmShopCarExample;
import com.cn.springcloudshop.model.YmShopProduct;
import com.cn.springcloudshop.service.inter.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Author: wuyanzu
 * @Date: 2018/7/31
 */
@Service("ShopCarService")
@Transactional(rollbackFor = Exception.class)
public class ShopCarServiceImpl implements ShopCarService {

    @Autowired
    YmShopCarMapper ymShopCarMapper;

    @Autowired
    YmShopProductMapper ymShopProductMapper;

    @Override
    public int deleteByPrimaryKey(Long shopCarId) {
        Assert.notNull(shopCarId,"购物车ID不能为空");
        Integer res = ymShopCarMapper.deleteByPrimaryKey(shopCarId);
        return res == null ? 0 : res;
    }

    @Override
    public int insert(YmShopCar record) {
        Assert.notNull(record,"购物车不能为空");
        Integer res = null;
        YmShopCarExample ymShopCarExample = new YmShopCarExample();
        ymShopCarExample.createCriteria().andShopCarProductIdEqualTo(record.getShopCarProductId()).andShopCarOpenidEqualTo(record.getShopCarOpenid());
        List<YmShopCar> shopCar = ymShopCarMapper.selectByExample(ymShopCarExample);
        if (shopCar != null && shopCar.size() > 0) {
            shopCar.get(0).setShopCarProductNumber(record.getShopCarProductNumber() + shopCar.get(0).getShopCarProductNumber());
            res = ymShopCarMapper.updateByPrimaryKey(shopCar.get(0));
        } else {
            res = ymShopCarMapper.insert(record);
        }
        return res == null ? 0 : res;
    }

    @Override
    public int insertSelective(YmShopCar record) {
        Assert.notNull(record,"购物车不能为空");
        Integer res = ymShopCarMapper.insertSelective(record);
        return res == null ? 0 : res;
    }

    @Override
    public List<YmShopCar> selectByExample(YmShopCarExample example) {
        Assert.notNull(example,"商品实例不能为空");
        List<YmShopCar> shopCars = ymShopCarMapper.selectByExample(example);
        return shopCars == null ? null : shopCars;
    }

    @Override
    public YmShopCar selectByPrimaryKey(Long shopCarId) {
        Assert.notNull(shopCarId,"购物车ID不能为空");
        YmShopCar res = ymShopCarMapper.selectByPrimaryKey(shopCarId);
        return res == null ? null : res;
    }

    @Override
    public int updateByPrimaryKeySelective(YmShopCar record) {
        Assert.notNull(record,"购物车不能为空");
        Integer res = ymShopCarMapper.updateByPrimaryKeySelective(record);
        return res == null ? 0 : res;
    }

    @Override
    public int updateByPrimaryKey(YmShopCar record) {
        Assert.notNull(record,"购物车不能为空");
        Integer res = ymShopCarMapper.updateByPrimaryKey(record);
        return res == null ? 0 : res;
    }

    @Override
    public int deleteBatchByPK(List<Long> shopCarIds) {
        Assert.notEmpty(shopCarIds,"购物车ID不能为空");
        Integer res = ymShopCarMapper.deleteBatchByPK(shopCarIds);
        return res == null ? 0 : res;
    }
}
